<?php

namespace Biz\model\course;

use Doraemon\pockets\datebase\BaseDbModel;

class CourseEvaluation extends BaseDbModel
{

    const _CONFIG_FILE_ = 'db_course';

    const _CONFIG_SELECT_ = 'db_course';

    const _DATABASE_ = 'hhz_course';

    const _TABLE_NAME = 'course_evaluation';

    public static function selectByWhere($where, $select = '*')
    {
        $list = self::select($select, $where);
        return $list;
    }

    public static function getByWhere($where)
    {
        return self::get('*', $where);
    }

    public static function selectCount($where)
    {
        return self::count($where);
    }

    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return self::id();
    }

    public static function updateData($data, $where)
    {
        $data['edittime'] = date('Y-m-d H:i:s');
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    public static function isEvaluated($courseIds,$uid)
    {
        $result = [];
        if (!is_array($courseIds)) {
            $courseIds = array($courseIds);
        }
        foreach ($courseIds as $courseId) {
            $result[$courseId] = 0;
        }
        $list = self::selectByWhere(['course_id' => $courseIds,'uid'=>$uid]);
        foreach ($list as $row) {
            $result[$row['course_id']] = 1;
        }
        return $result;
    }

    public static function getCourseAvgStar(array $courseIds)
    {
        $courseId = implode(',', $courseIds);
        $sql = "select `course_id`, AVG(`course_start`) as `course_start`, AVG(`teacher_start`) as `teacher_start` ,AVG(`ue_start`) as `ue_start` from `course_evaluation` where `course_id` in({$courseId}) group by course_id";
        $result = self::query($sql)->fetchAll();
        $list = [];
        foreach ($result as $row) {
            $list[$row['course_id']] = [
                'course_id' => $row['course_id'],
                'course_start' => $row['course_start'],
                'teacher_start' => $row['teacher_start'],
                'ue_start' => $row['ue_start']
            ];
        }
        return $list;
    }

}


